/* Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sairobo.base.common.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 日期帮助类
 * 
 * @version 1.0
 */
public class DateHelper
{

	protected static Logger logger = LoggerFactory.getLogger(DateHelper.class);

	public static final String DATE_FORMAT_1 = "MM/dd/yyyy HH:mm:ss";

	public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_FORMAT_3 = "yyyy-MM-dd";

	public static final String DATE_FORMAT_4 = "yyyyMMddHHmmss";

	public static final String DATE_FORMAT_5 = "HH:mm:ss";

	public static final String DATE_FORMAT_6 = "yyyyMMdd";

	public static final String DATE_FORMAT_7 = "yyyy-MM";

	// 日期格式化
	private static DateFormat dateFormat = null;

	public static String getTime(String format)
	{
		return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
	}

	public static String transformFormat(String time, String srcFmt, String tarFmt)
	{
		if (StringUtils.isNotEmpty(time))
		{
			return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(srcFmt)).format(DateTimeFormatter.ofPattern(tarFmt));
		}
		return "";

	}

	public static String transformDate(Date date, String fmt)
	{

		if (date == null || StringUtils.isEmpty(fmt))
		{
			return "";
		}
		return new SimpleDateFormat(fmt).format(date);
	}

	public static boolean equalsDate(Date date1, Date date2, int field)
	{

		Date dateOne = DateUtils.truncate(date1, field);
		Date dateTwo = DateUtils.truncate(date2, field);
		return dateOne.compareTo(dateTwo) == 0;
	}

	public static Date parseDateTime(String dateStr, String fmt) throws ParseException
	{
		if (StringUtils.isEmpty(fmt) || StringUtils.isEmpty(dateStr))
		{
			return null;
		}
		return new SimpleDateFormat(fmt).parse(dateStr);
	}


	/**
	 * 对时限数据进行处理 1、运行时设置的date型数据直接返回 2、模型设置的需要特殊转换成date类型 3、运行时设置的转换为date型
	 * 
	 * @param args
	 *            运行时参数
	 * @param parameter
	 *            模型参数
	 * @return Date类型
	 */
	public static Date processTime(Map<String, Object> args, String parameter)
	{
		if (StringUtils.isEmpty(parameter))
			return null;
		Object data = args.get(parameter);
		if (data == null)
			data = parameter;

		Date result = null;
		if (data instanceof Date)
		{
			return (Date) data;
		}
		else if (data instanceof Long)
		{
			return new Date((Long) data);
		}
		else if (data instanceof String)
		{
			// TODO 1.4-dev ignore
		}
		return result;
	}

	/**
	 * 设置毫秒数为0返回时间
	 */
	public static Date getDateByCalendar(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 返回星期几
	 */
	public static int getWeekDay(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek;
	}

	public static Date StringTodate(String string)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");// 小写的mm表示的是分钟
		Date date = null;
		try
		{
			date = sdf.parse(string);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return date;
	}

	public static String DateToString(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(date);
		return str;
	}

	// public static String DateToStringYM(Date date){
	// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
	// DateUtil.format(date,"yyyy-MM");
	// String str=sdf.format(date);
	// return str;
	// }

	/**
	 * 日期格式化
	 *
	 * @param date
	 * @return
	 */
	public static Date getDateFormat(String date)
	{
		try
		{
			return dateFormat.parse(date);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前日期(yyyy-MM-dd)
	 * 
	 * @return
	 */
	public static Date getNowDate()
	{
		return DateHelper.getDateFormat(dateFormat.format(new Date()));
	}


}
